package com.secxiot.provider.narcotic.controller;

import com.secxiot.api.narcotic.NarcoticApi;
import com.secxiot.common.annotation.Log;
import com.secxiot.common.entity.ResultVo;
import com.secxiot.provider.narcotic.dto.NarcoticPatientDrugsRecordDto;
import com.secxiot.provider.narcotic.entity.NarcoticDocDo;
import com.secxiot.provider.narcotic.entity.TakeDestroyDo;
import com.secxiot.provider.narcotic.service.NarcoticService;
import com.secxiot.provider.narcotic.vo.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController("narcotics")
public class NarcoticProviderController implements NarcoticApi {

    private static Logger LOG = LoggerFactory.getLogger(NarcoticProviderController.class);

    @Autowired
    private NarcoticService narcoticService;

    @Log("医药须知")
    @GetMapping("/narcoticDoc")
    @Override
    public NarcoticDocDo getNarcoticDoc(@RequestParam(value = "hospitalId", required = false) Long hospitalId,
                                        @RequestParam(value = "type", required = false) String type) {
        return narcoticService.selectNarcoticDoc(hospitalId, type);
    }

    @Log("医药信息")
    @GetMapping("/narcoticDrugs")
    @Override
    public List<NarcoticDrugsVo> selectNarcoticDrugs(@RequestParam(value = "hospitalId", required = true) Long hospitalId) {
        return narcoticService.selectNarcoticDrugs(hospitalId);
    }

    @Log("医药类型")
    @GetMapping("/getNarcoticDrugsById")
    @Override
    public NarcoticDrugsVo getNarcoticDrugsById(@RequestParam(value = "drugsId", required = true) Long drugsId) {
        return narcoticService.getNarcoticDrugsById(drugsId);
    }

    @Log("用户资料信息--分页列表")
    @GetMapping("/narcoticUserInfo/pageList")
    @Override
    public List<NarcoticPatientInfoVo> selectNarcoticPatientInfoPage(@RequestParam(value = "userId", required = true) Long userId,
                                                                     @RequestParam(value = "pageNo", required = true) Integer pageNo,
                                                                     @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        return narcoticService.selectNarcoticPatientInfoPage(userId, pageNo, pageSize);
    }

    @Log("用户资料信息--列表")
    @GetMapping("/narcoticUserInfo/list")
    @Override
    public List<NarcoticPatientInfoVo> selectNarcoticPatientInfoList(@RequestParam(value = "userId", required = true) Long userId) {
        return narcoticService.selectNarcoticPatientInfoList(userId);
    }

    @Log("用户资料信息--详情")
    @GetMapping("/narcoticUserInfo/detail")
    @Override
    public NarcoticPatientInfoVo getNarcoticPatientInfoById(@RequestParam(value = "patientInfoId", required = true) Long patientInfoId) {
        return narcoticService.getNarcoticPatientInfoById(patientInfoId);
    }

    @Log("用户资料信息--添加")
    @PostMapping("/narcoticUserInfo/insert")
    @Override
    public Long insertNarcoticPatientInfo(NarcoticPatientInfoVo narcoticPatientInfoVo) {
        return narcoticService.insertNarcoticPatientInfo(narcoticPatientInfoVo);
    }

    @Log("用户资料信息--修改")
    @PostMapping("/narcoticUserInfo/update")
    @Override
    public Long updateNarcoticPatientInfo(NarcoticPatientInfoVo narcoticUserInfoVo) {
        return narcoticService.updateNarcoticPatientInfo(narcoticUserInfoVo);
    }

    @Log("申请药品记录--列表")
    @GetMapping("/narcoticDrugsRecord/list")
    @Override
    public List<NarcoticPatientDrugsRecordVo> selectNarcoticPatientDrugsRecord(@RequestParam(value = "userId", required = true) Long userId,
                                                                               @RequestParam(value = "pageNo", required = false) Integer pageNo,
                                                                               @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                                               @RequestParam(value = "isPage", required = true) Integer isPage,
                                                                               @RequestParam(value = "searchName", required = false) String searchName) {
        return narcoticService.selectNarcoticPatientDrugsRecord(userId, pageNo, pageSize, isPage, searchName);
    }

    @Log("申请药品记录--添加")
    @PostMapping("/narcoticDrugsRecord/insert")
    @Override
    public Long insertNarcoticPatientDrugsRecord(NarcoticPatientDrugsRecordVo narcoticPatientDrugsRecordVo) {
        return narcoticService.insertNarcoticDrugsRecord(narcoticPatientDrugsRecordVo);
    }

    @Log("申请药品记录--修改")
    @PostMapping("/narcoticDrugsRecord/update")
    @Override
    public Long updateNarcoticPatientDrugsRecord(NarcoticPatientDrugsRecordVo narcoticPatientDrugsRecordVo) {
        return narcoticService.updateNarcoticDrugsRecord(narcoticPatientDrugsRecordVo);
    }

    @ApiOperation(value = "医生药品记录--详情", httpMethod = "GET")
    @GetMapping("/narcoticDoctorRecord/detail")
    @Override
    public NarcoticDoctorRecordVo getNarcoticDoctorRecordById(@RequestParam(value = "doctorRecordId", required = true) Long doctorRecordId) {
        return narcoticService.getNarcoticDoctorRecordById(doctorRecordId);
    }

    /**
     * 医生药品记录 列表
     *
     * @param doctorUserId 医生用户ID
     * @param hospitalId   医疗机构ID
     * @param pageNo       页数
     * @param pageSize     条数
     * @param isPage       是否分页  0 否  1 是
     * @param searchName   关键字
     * @return
     */
    @ApiOperation(value = "医生药品记录--列表", httpMethod = "GET")
    @GetMapping("/narcoticDoctorRecord/list")
    @Override
    public List<NarcoticDoctorRecordVo> selectNarcoticDoctorRecordList(@RequestParam(value = "doctorUserId", required = true) Long doctorUserId,
                                                                       @RequestParam(value = "hospitalId", required = true) Long hospitalId,
                                                                       @RequestParam(value = "pageNo", required = false) Integer pageNo,
                                                                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                                       @RequestParam(value = "isPage", required = true) Integer isPage,
                                                                       @RequestParam(value = "searchName", required = false) String searchName) {
        return narcoticService.selectNarcoticDoctorRecordList(doctorUserId, hospitalId, pageNo, pageSize, isPage, searchName);
    }

    /**
     * 医生药品记录 审核
     *
     * @return
     */
    @ApiOperation(value = "医生药品记录--审核", httpMethod = "POST")
    @PostMapping("/narcoticDoctorRecord/audit")
    @Override
    public ResultVo<NarcoticDoctorRecordVo> auditNarcoticDoctorRecord(NarcoticDoctorRecordVo narcoticDoctorRecordVo) {
        return narcoticService.auditNarcoticDoctorRecord(narcoticDoctorRecordVo);
    }

    @ApiOperation(value = "药师药品记录--详情", httpMethod = "GET")
    @GetMapping("/narcoticChemistRecord/detail")
    @Override
    public NarcoticChemistRecordVo getNarcoticChemistRecordById(@RequestParam(value = "chemistRecordId", required = true) Long chemistRecordId) {
        return narcoticService.getNarcoticChemistRecordById(chemistRecordId);
    }

    /**
     * 药师药品记录 列表
     *
     * @param chemistUserId 药师用户ID
     * @param hospitalId    医疗机构ID
     * @param pageNo        页数
     * @param pageSize      条数
     * @param isPage        是否分页  0 否  1 是
     * @param searchName    关键字
     * @return
     */
    @ApiOperation(value = "药师药品记录--列表", httpMethod = "GET")
    @GetMapping("/narcoticChemistRecord/list")
    @Override
    public List<NarcoticChemistRecordVo> selectNarcoticChemistRecordList(@RequestParam(value = "chemistUserId", required = true) Long chemistUserId,
                                                                         @RequestParam(value = "hospitalId", required = true) Long hospitalId,
                                                                         @RequestParam(value = "pageNo", required = false) Integer pageNo,
                                                                         @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                                         @RequestParam(value = "isPage", required = true) Integer isPage,
                                                                         @RequestParam(value = "searchName", required = false) String searchName) {
        return narcoticService.selectNarcoticChemistRecordList(chemistUserId, hospitalId, pageNo, pageSize, isPage, searchName);
    }

    /**
     * 药师药品记录 审核
     *
     * @return
     */
    @ApiOperation(value = "药师药品记录--审核", httpMethod = "POST")
    @PostMapping("/narcoticChemistRecord/audit")
    @Override
    public ResultVo<NarcoticChemistRecordVo> auditNarcoticChemistRecord(NarcoticChemistRecordVo narcoticChemistRecordVo) {
        return narcoticService.auditNarcoticChemistRecord(narcoticChemistRecordVo);
    }

    /**
     * 药师重置审核
     *
     * @return
     */
    @ApiOperation(value = "药师重置审核", httpMethod = "POST")
    @PostMapping("/narcoticChemistRecord/resetAudit")
    @Override
    public Long resetAuditNarcoticChemistRecord(Long chemistRecordId, Long chemistUserId) {
        return narcoticService.resetAuditNarcoticChemistRecord(chemistRecordId, chemistUserId);
    }

    /**
     * 取药销毁记录 信息
     *
     * @param takeDestroyId 取药记录id
     * @return
     */
    @ApiOperation(value = "取药销毁记录--详情", httpMethod = "GET")
    @GetMapping("/narcoticTakeDestroy/detail")
    @Override
    public NarcoticTakeDestroyVo getNarcoticTakeDestroyById(@RequestParam("takeDestroyId") Long takeDestroyId) {
        return narcoticService.getNarcoticTakeDestroyById(takeDestroyId);
    }

    /**
     * 取药销毁记录 列表
     *
     * @param hospitalId 医疗机构ID
     * @param type       1 取药 2 销毁
     * @param pageNo     页数
     * @param pageSize   条数
     * @param isPage     是否分页  0 否  1 是
     * @param searchName 关键字
     * @return
     */
    @ApiOperation(value = "取药销毁记录--列表", httpMethod = "GET")
    @GetMapping("/narcoticTakeDestroy/list")
    @Override
    public List<NarcoticTakeDestroyVo> selectNarcoticTakeDestroy(@RequestParam("hospitalId") Long hospitalId,
                                                                 @RequestParam("type") String type,
                                                                 @RequestParam("pageNo") Integer pageNo,
                                                                 @RequestParam("pageSize") Integer pageSize,
                                                                 @RequestParam("isPage") Integer isPage,
                                                                 @RequestParam("searchName") String searchName) {
        return narcoticService.selectNarcoticTakeDestroy(hospitalId, type, pageNo, pageSize, isPage, searchName);
    }

    /**
     * 患者取药
     *
     * @param takeDestroyDo 取药入参
     * @return
     */
    @ApiOperation(value = "取药", httpMethod = "POST")
    @PostMapping("/narcoticTakeDestroy/takeMedicine")
    @Override
    public ResultVo<NarcoticTakeDestroyVo> auditNarcoticTakeMedicine(@RequestBody TakeDestroyDo takeDestroyDo) {
        return narcoticService.auditNarcoticTakeMedicine(takeDestroyDo);
    }

    /**
     * 销毁药瓶
     *
     * @param id
     * @param destroyFile   销毁附件
     * @param destroyUserId 销毁人
     * @return
     */
    @ApiOperation(value = "销毁药瓶", httpMethod = "GET")
    @GetMapping("/narcoticTakeDestroy/destoryMedicine")
    @Override
    public ResultVo<NarcoticTakeDestroyVo> auditNarcoticDestroy(@RequestParam("id") Long id,
                                                                @RequestParam("destroyFile") String destroyFile,
                                                                @RequestParam("destroyUserId") Long destroyUserId) {
        NarcoticTakeDestroyVo narcoticTakeDestroyVo = new NarcoticTakeDestroyVo();
        narcoticTakeDestroyVo.setTakeDestroyId(id);
        narcoticTakeDestroyVo.setDestroyFile(destroyFile);
        //narcoticTakeDestroyVo.setDestroyUserId(destroyUserId);
        return narcoticService.auditNarcoticDestroy(narcoticTakeDestroyVo);
    }

    /**
     * 监管审核资料审核
     *
     * @param narcoticUserInfoVo
     * @return
     */
    @ApiOperation(value = "监管资料--审核", httpMethod = "POST")
    @PostMapping("/narcoticUserInfo/superviseAudit")
    @Override
    public ResultVo<NarcoticPatientInfoVo> auditNarcoticPatientInfo(NarcoticPatientInfoVo narcoticUserInfoVo) {
        return narcoticService.auditNarcoticPatientInfo(narcoticUserInfoVo);
    }

    /**
     * 监管资料列表
     *
     * @param hospitalId 医疗机构ID
     * @param pageNo     页数
     * @param pageSize   条数
     * @param isPage     是否分页  0 否  1 是
     * @return 用户资料信息
     */
    @ApiOperation(value = "监管资料列表--列表", httpMethod = "GET")
    @GetMapping("/narcoticUserInfo/superviseList")
    @Override
    public List<NarcoticPatientInfoVo> selectNarcoticPatientInfoPageByHospital(
            @RequestParam(value = "hospitalId") Long hospitalId,
            @RequestParam(value = "pageNo") Integer pageNo,
            @RequestParam(value = "pageSize") Integer pageSize,
            @RequestParam(value = "isPage") Integer isPage,
            @RequestParam(value = "searchName") String searchName) {
        return narcoticService.selectNarcoticPatientInfoPageByHospital(hospitalId, pageNo, pageSize, isPage, searchName);
    }

    /**
     * 药品跟踪记录--详情
     *
     * @param patientDrugsRecordId 申请用药记录ID
     * @return NarcoticDrugsRecordDo
     */
    @ApiOperation(value = "药品跟踪记录--详情", httpMethod = "GET")
    @GetMapping("/narcoticDrugsRecordDto/detail")
    @Override
    public ResultVo<NarcoticPatientDrugsRecordDto> getNarcoticPatientDrugsRecordDtoById(@Param("patientDrugsRecordId") Long patientDrugsRecordId) {
        return narcoticService.getNarcoticPatientDrugsRecordDtoById(patientDrugsRecordId);
    }

    /**
     * 药品厂家列表
     *
     * @param hospitalId 医院ID
     * @return
     */
    @ApiOperation(value = "药品厂家--列表", httpMethod = "GET")
    @GetMapping("/narcoticDrugsFactory/list")
    @Override
    public List<NarcoticDrugsFactoryVo> getNarcoticDrugsFactoryList(@RequestParam("hospitalId") Long hospitalId) {
        return narcoticService.getNarcoticDrugsFactoryList(hospitalId);
    }

    /**
     * 扫码验证药品
     *
     * @param patientDrugsRecordId 申请用药记录Id
     * @param patientUserId        患者用户Id
     * @return
     */
    @ApiOperation(value = "扫码验证药品", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientDrugsRecordId", value = "申请用药记录Id", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "patientUserId", value = "患者用户Id", required = true, paramType = "query", dataType = "long"),

    })
    @GetMapping("/scanDrugsQRCode")
    @Override
    public QRCodeProDrugsVo scanDrugsQRCode(@RequestParam("patientDrugsRecordId") Long patientDrugsRecordId,
                                            @RequestParam("patientUserId") Long patientUserId) {
        return narcoticService.scanDrugsQRCode(patientDrugsRecordId, patientUserId);
    }

    /**
     * 获取药品统计数据
     *
     * @param userId   用户ID
     * @param dateTime 时间节点
     * @return
     */
    @GetMapping("/getDrugsCountResultVo")
    @Override
    public DrugsCountResultVo getDrugsCountResultVo(@RequestParam("userId") Long userId, @RequestParam("dateTime") Date dateTime) {
        return narcoticService.getDrugsCountResultVo(userId, dateTime);
    }

    /**
     * 获取用户有效资料
     *
     * @param userId 用户Id
     */
    @GetMapping("/getPatientInfoByUserId")
    @Override
    public List<NarcoticPatientInfoVo> getPatientInfoByUserId(@RequestParam("userId") Long userId) {
        return narcoticService.getPatientInfoByUserId(userId);
    }

    /**
     * 获取用户未完成的用药申请列表
     *
     * @param userId 用户Id
     */
    @GetMapping("/getPatientDrugsRecordByUserId")
    @Override
    public List<NarcoticPatientDrugsRecordVo> getPatientDrugsRecordByUserId(@RequestParam("userId") Long userId) {
        return narcoticService.getPatientDrugsRecordByUserId(userId);
    }

}
